package com.xuebusi.mapper;

import com.xuebusi.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * Created by SYJ on 2017/11/11.
 */
@Mapper
public interface UserMapper {

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Select({
            "select id, username, age, birthday, address, gender, phone, headimg ",
            "from x_user ",
            "where id = #{id, jdbcType=INTEGER}",
            "and delflag = 0"
    })
    @Results({
            @Result(column="id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "username", property = "username", jdbcType = JdbcType.VARCHAR),
            @Result(column = "age", property = "age", jdbcType = JdbcType.INTEGER),
            @Result(column = "birthday", property = "birthday", jdbcType = JdbcType.DATE),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.INTEGER),
            @Result(column = "headimg", property = "headimg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR)
    })
    User selectById(Integer id);

    /**
     * 查询所有用户
     * @return
     */
    @Select({
            "select id, username, age, birthday, address, gender, phone, headimg ",
            "from x_user",
            "where delflag = 0"
    })
    @Results({
            @Result(column="id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "username", property = "username", jdbcType = JdbcType.VARCHAR),
            @Result(column = "age", property = "age", jdbcType = JdbcType.INTEGER),
            @Result(column = "birthday", property = "birthday", jdbcType = JdbcType.DATE),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.INTEGER),
            @Result(column = "headimg", property = "headimg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "phone", property = "phone", jdbcType = JdbcType.VARCHAR)
    })
    List<User> findAll();

    @Insert({
            "insert x_user(username, age, birthday, address, gender, phone, headimg, delflag)",
            "values(#{username, jdbcType=VARCHAR}, ",
            "#{age, jdbcType=INTEGER}, ",
            "#{birthday, jdbcType=DATE}, ",
            "#{address, jdbcType=VARCHAR}, ",
            "#{gender, jdbcType=INTEGER}, ",
            "#{phone, jdbcType=VARCHAR},",
            "#{headimg, jdbcType=VARCHAR},",
            "#{delflag, jdbcType=INTEGER})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(User user);

    @Update({
        "update x_user",
        "set username=#{username, jdbcType=VARCHAR},",
        "age=#{age, jdbcType=INTEGER},",
        "birthday=#{birthday, jdbcType=DATE},",
        "address=#{address, jdbcType=VARCHAR},",
        "gender=#{gender, jdbcType=INTEGER},",
        "phone=#{phone, jdbcType=VARCHAR},",
        "headimg=#{headimg, jdbcType=VARCHAR},",
        "delflag=#{delflag, jdbcType=INTEGER}",
        "where id=#{id, jdbcType=INTEGER}"
    })
    int update(User user);


    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Delete({
            "delete from x_user ",
            "where id = #{id, jdbcType=INTEGER}"
    })
    int deleteById(Integer id);
}
